package com.example.his.api.mis.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.GoodsDao;
import com.example.his.api.mis.controller.form.SearchGoodsByPageForm;
import com.example.his.api.mis.service.GoodsService;
import com.example.his.api.utils.MinioUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author huixh
 */
@Service("misGoodsServiceImpl")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private MinioUtil minioUtil;
    @Override
    public PageUtils searchByPage(Map<String, Object> map) {
        List<?> list = new ArrayList<>();
        long count = goodsDao.searchCount(map);
        if (count > 0) {
            list = goodsDao.searchByPage(map);
        }
        int start = MapUtil.getInt(map, "start");
        int length = MapUtil.getInt(map, "length");

        return new PageUtils(count, length, start, list);
    }

    @Override
    public String uploadImage(MultipartFile file) {
        String fileName = IdUtil.simpleUUID();
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();

        // 获取文件后缀名
        String fileExtension = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        // 拼接 UUID 和后缀名
        String fullFileName = fileName + fileExtension;
        String path = "front/goods/" + fullFileName;
        minioUtil.uploadImage(path, file);
        return fullFileName;
    }
}
